package com.revolver.base;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @Auther: wzn
 * @Date: 2018/6/1 15:45
 * @Description: 基于通用MyBatis Mapper插件的Service接口的实现
 */
@Service
@Transactional
public abstract class AbstractService<T, Q extends QueryEntity> implements BaseService<T, Q> {
    protected static Logger logger = LoggerFactory.getLogger(AbstractService.class);

    @Autowired
    protected Mapper<T, Q> mapper;

    private Class<T> tClass;    // 当前泛型真实类型的Class

    private Class<Q> qClass;    // 当前泛型真实类型的Class

    public AbstractService() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        tClass = (Class<T>) pt.getActualTypeArguments()[0];
        qClass = (Class<Q>) pt.getActualTypeArguments()[1];
    }

    @Override
    public int deleteByPrimaryKey(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    @Override
    public int delete(T record) {
        return mapper.delete(record);
    }

    @Override
    public int insert(T record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(T record) {
        return mapper.insertSelective(record);
    }

    @Transactional(readOnly = true)
    @Override
    public boolean existsWithPrimaryKey(Object key) {
        return mapper.existsWithPrimaryKey(key);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    @Transactional(readOnly = true)
    @Override
    public T selectByPrimaryKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    @Transactional(readOnly = true)
    @Override
    public int selectCount(T record) {
        return mapper.selectCount(record);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> select(T record) {
        return mapper.select(record);
    }

    @Transactional(readOnly = true)
    @Override
    public T selectOne(T record) {
        return mapper.selectOne(record);
    }


    @Override
    public int updateByPrimaryKey(T record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByExample(Object example) {
        return mapper.deleteByExample(example);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    @Override
    public int selectCountByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    @Transactional(readOnly = true)
    @Override
    public T selectOneByExample(Object example) {
        return mapper.selectOneByExample(example);
    }

    @Override
    public int updateByExample(T record, Object example) {
        return mapper.updateByExample(record, example);
    }

    @Override
    public int updateByExampleSelective(T record, Object example) {
        return mapper.updateByExampleSelective(record, example);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds) {
        return mapper.selectByExampleAndRowBounds(example, rowBounds);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectByRowBounds(T record, RowBounds rowBounds) {
        return mapper.selectByRowBounds(record, rowBounds);
    }

    @Override
    public int insertList(List<T> recordList) {
        return mapper.insertList(recordList);
    }

    @Override
    public int insertUseGeneratedKeys(T record) {
        return mapper.insertUseGeneratedKeys(record);
    }
}
